<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	
	
	
	//分离URL以及ID
	$id = $tid;
	//定义公用变量
	if (substr($url,-1)!="/")
		$url.="/";
	$url_pre = $url;
	$url = 'http://mh.xindm.cn/type.asp?typeid=' . $tid;
	$pic_url_pre = 'http://mh.xindm.cn/book';
	$pic_file_pre = 'xindm_cn_';
		
		
	//获取URL内容
	//$url = 'E:\web\hf\xindm\test\type.html';
	
	
	
	
	$content = 	file_get_contents($url);
	$content = 	mb_convert_encoding($content,"UTF-8","GB2312");
	
	//获取名称
	$name = explode('>> ',$content);
	$name = $name[1];
	$name = explode('</title>',$name);
	$name = $name[0];
	$name = explode('[',$name);
	$name = $name[0];
	
	
	//获取最后更新时间
	$dt = explode('更新时间：',$content);
	$dt = $dt[1];
	$dt = explode('</span>',$dt);
	$dt = strtotime(trim($dt[0]));
	
	//获取URL列表
	$urls = explode('<table width="100%" border="0" cellspacing="0" cellpadding="9">',$content);
	$urls = $urls[1];
	$urls = explode('</table>',$urls);
	$urls = $urls[0];
	
	//'<a href='http://mh.xindm.cn/display.asp?id=63288' target='_blank'><font color='#ff5500'>';
	//提高精确度用，此处无需使用
	//$replacer = array('target="_blank"','<span>','</span>',' class="tpy"','页');
	//$replacerc = array('','','','','');
	//$urls = str_replace($replacer,$replacerc,$urls);
	
	$match_str = "/a>  <a href=\'(.*)\' target=\'\_blank\'><font/isU";
	preg_match_all($match_str,$urls,$urln);
	$urln = $urln[1];
	//print_r($urln);exit;
	
	//获取每单位总数(主要用于倒序)
	//$match_str = "/\((.*)\)/isU";
	//preg_match_all($match_str,$urls,$numn);
	//$numn = $numn[1];
	$num = count($urln);
	//$ulnum = array($urln,$numn);
	//进入每页进行处理
	
	//插入分类信息
		//检查当前ID是否存在
		$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => 'xindmtags','where'	=> "1 " . $where ,)
		);	
		//当已经存在的时候更新日期
		if ($result['max'] > 0) {
			
		}
		//当不存在时插入
		else{
			do_taginsert($id,$dt,$name,0);
		}
	//获取数据库COUNT
	$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'count','from'   => 'xindmtags','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['count'];
	
	//由于是逆序，需要倒装数组(链接以及每个链接的数量，注意，XINDM由于连接数量需要进入下一级页面采集，这里无需倒装)
	$urln = array_reverse($urln);
	//$numn = array_reverse( $numn );
	
	
	foreach ($urln as $k=>$v) {
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		//单集漫画采集
		$current_url = $urln[$k];
		
		//print_r($current_url);exit;
		
		//$current_url = 'E:\web\hf\xindm\test\dis.html';
		
		$s_pic_content = mb_convert_encoding(file_get_contents($current_url),"UTF-8","GB2312");
		$res = get_comic_pics($s_pic_content,$pic_url_pre,$pic_file_pre,$current_url);
		//$res = get_comic_pics($pic_url_pre,$pic_file_pre,$current_url);
// 		print_r($res);exit;
		//这里添加数据库插入语句
		foreach ($res as $v) {
			//直接桉页插入
			do_photoinsert($id,$v,1);
		}
	}
	//更新计数
	$where = 'id= ' . $id .'';
	$DB->do_update('xindmtags',
		array(					  
		   'lastupdate'				=> $dt,
		   'count'					=> count($urln),
		 ),
		 $where
	);
	
	
	
	
	function get_comic_pics($content,$pic_url_pre,$pic_file_pre,$current_url = ''){
		//获取总页数
		$tp = explode('共<strong><font color="#FF0000">',$content);
		$tp = $tp[1];
		$tp = explode('</font>',$tp);
		$tp = $tp[0];
		//获取每页连接
		//file_put_contents('E:\web\hf\xindm\test\123.html', $content);exit;
		$res = array();
		for ($i = 1;$i<$tp;$i++){
			$current_pic_page = $current_url . '&page=' . $i;
			//单独进入每页采集
			$content = mb_convert_encoding(file_get_contents($current_pic_page),"UTF-8","GB2312");
			//获取图片连接前缀
			$picurl = explode('../book',$content);
			$picurl = $picurl[1];
			$picurl = explode('" ',$picurl);
			$picurl = $pic_url_pre . $picurl[0];
			$res[$i - 1] = $picurl;
		}
// 		$picurl = $pic_url_pre . $picurl . $pic_file_pre;
// 		$res = array($tp,$picurl);
		return $res;
	}
	
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( 'xindmtags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count){
		global $DB;
		$DB->do_insert( 'xindmphotos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	echo "采集完毕";
	
	
	exit;
	
	
?>